import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import { appReportUrl } from '../../report.url';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { report } from '../../report.data';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { InitService } from '../../../public/serve/init/init.service';
import {appSynonymUrl} from '../../../synonym/synonym.url';

@Component({
  selector: 'app-report-product-table',
  templateUrl: './report-product-table.component.html'
})
export class ReportProductTableComponent implements OnInit {

  total      : number = 0;
  naturalNum : number = 0;
  tabVal : Array<object>;

  isSelectAll : boolean = false;
  isChose : boolean = false;

  constructor(
    private server: Service,
    public request: CudTableService,
    private tableListService: CudTableService,
    private hintsService: HintsService,
    private removeTableService: RemoveTableService,
    public initService: InitService,
  ) { }

  ngOnInit() {
    report.retrieval.queryCondition = "";
    report.pagination.pageNo = 1;
    report.pagination.pageSize = 20;
    this.getIndexingProductList();
  }

  /**
   * 产品内容列表
   */
  getIndexingProductList(){
    
    let data = this._tableData();
    this.request.postRequest(this.server.baseUrl +appReportUrl.getIndexingProductList,data).then((res:any) => {
      if (res.result_code === '0000') {
        this.tabVal = res.result;
        this.total = res.length;
        this.naturalNum = res.length;
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }

      this._cancelChecked();
      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      this._cancelChecked();
      return;
    });
  }

  /**
   * 全选
   * @param bool
   * @param tabVal
   */
  _selectAll(bool,tabVal){
    this.isSelectAll = this.isChose = this.removeTableService.removeAll(bool, tabVal, 'id');
    if (this.isSelectAll == false) {
      this.getIndexingProductList();
    }
  }

   /**
    * 单选
    * @param bool
    * @param synonymId
    * @param length
    */
   _chose(bool, synonymId, length){
    this.isSelectAll = this.removeTableService.removeChose(bool, synonymId, length)['bool'];
   }

  /**
   * 重置表格复选框、选中的ID
   */
  private _cancelChecked(){
    this.isChose = this.isSelectAll = false;
    this.removeTableService._clearId("id");
  }

  //查询参数
  private _tableData(){
    return {
      token         : report.retrieval.token,
      pageNo         : report.pagination.pageNo,
      pageSize       : report.pagination.pageSize,
      queryCondition : report.retrieval.queryCondition,
    }
  }
}
